package cn.eoeit.alone.phoneImport;


import java.util.ArrayList;

import cn.eoeit.alone.phoneImport.db.CacheDbAgent;
import cn.eoeit.alone.phoneImport.db.CacheDbAgentImpl;

import android.app.Activity;  
import android.app.Application;
 
public class SysApplication extends  Application { 
    private ArrayList<Activity> mList = new ArrayList<Activity>(); 
    private ArrayList<Activity> detailList = new ArrayList<Activity>();
    private static CacheDbAgent cacheDbAgent; 
     
    public CacheDbAgent getCacheDbAgent() {
		if(cacheDbAgent==null){
			cacheDbAgent=new CacheDbAgentImpl(SysApplication.this);
		}
		return cacheDbAgent;
	}

    // add Activity  
    public void addActivity(Activity activity) { 
        mList.add(activity); 
    } 
 
    public void exit() { 
        try { 
            for (Activity activity : mList) { 
                if (activity != null) 
                    activity.finish(); 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            System.exit(0); 
        } 
    } 
    
    public void addDetailActivity(Activity activity) {
    	detailList.add(activity);
    }
    
    /**
     * 
     */
    public void removeExtraActivity() {
    	if (detailList.size() > 2) {
    		detailList.get(0).finish();
    		detailList.remove(0);
    	}
    }
    
    public void onLowMemory() { 
        super.onLowMemory();     
        System.gc(); 
    }  
    
    @Override
    public void onCreate() {
    	super.onCreate();
    	cacheDbAgent=new CacheDbAgentImpl(this);
    }
    
    @Override
    public void onTerminate() {
        super.onTerminate();
    }
}

